** Distribution of individual village outcomes by Treatment Expansion Group
* Sample used to study household outcomes

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* keeping balanced panel
keep if ex_post_balp==1

* keeping only treatment expansion group
keep if c_b2009==1

* Easier labelling for graphs
la var w_real_sav "savings (INR)"
la var w_real_lent "money lent out (INR)"
la var w_real_borrow "money borrowed (INR)"
la var w_real_cons "total consumption (INR)"
la var w_real_food_exp "food expenditure (INR)"
la var w_real_nonfood_exp "non-food expenditure (INR)"
la var w_real_durables_exp "durables expenditure (INR)"
la var w_real_events_exp "events expenditure (INR)"
la var w_real_income "annual income(INR)"
la var w_real_gifts_out "gifts/transfers made (INR)"
la var w_real_gifts_in "gifts/transfers received (INR)"
la var food_sufficiency_child "food sufficiency for children"
la var fladder_std "financial situation (sd units)"
la var outlook_std "good things happen (sd units)"
la var control_std "control over life (sd units)"

replace Tvillage = 0  if mkt_year == 2005
sort id mkt_year

// CDFs
foreach var of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
areg `var', a(mkt_year)
predict e`var'yhat, residuals
qui sum `var'
gen `var'yhat = r(mean)+e`var'yhat
}

foreach var of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
cumul `var'yhat , gen(CDF_`var'_T) equal
lab var CDF_`var'_T "Treatment Expansion Group "
}


foreach var of varlist w_real_cons w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std {
local l`var': variable label `var'
twoway (line CDF_`var'_T `var'yhat, sort), title("`l`var''", size(s)) ytitle("") xlabel(,angle (30)) xtitle("") graphregion(color(white)) plotregion(icolor(white))yscale(range(0 1))
graph save cdfs`var', replace 
}

grc1leg cdfsw_real_sav.gph cdfsw_real_lent.gph cdfsw_real_borrow.gph cdfsw_real_cons.gph cdfsw_real_food_exp.gph cdfsw_real_nonfood_exp.gph cdfsw_real_durables_exp.gph cdfsw_real_events_exp.gph  cdfsw_real_gifts_out.gph cdfsw_real_income.gph cdfsw_real_gifts_in.gph cdfsfood_sufficiency_child.gph cdfsfladder_std.gph cdfsoutlook_std.gph cdfscontrol_std.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../output/graphs/a5b.emf", replace


